home *** CD-ROM | disk | FTP | other *** search
/ Delphi Informant Complete 1995 - 2000 / Delphi Informant Complete 1995 to 2000.iso / Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar / 1998 / Mar / di9803cc / MMButton.pas next >
Pascal/Delphi Source File  |  1997-07-22  |  7KB  |  245 lines

  1. unit MMButton;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, Classes, Graphics, Controls;
  7.  
  8. type
  9.   TSoundType = (stAppResource, stDLLResource, stFile);
  10.   TMMButton = class(TGraphicControl)
  11.   private
  12.     { Private declarations }
  13.     FBmpNormal,
  14.     FBmpHiLight,
  15.     FBmpPushed,
  16.     FBmpDisabled      : TBitmap;
  17.  
  18.     FSndOver,
  19.     FSndPush          : string;
  20.  
  21.     FSoundType        : TSoundType;
  22.     FDllInstance      : integer;
  23.  
  24.     FDown,
  25.     FOver             : Boolean;
  26.  
  27.     procedure setNormal(Value : TBitMap);
  28.     procedure setHiLight(Value : TBitMap);
  29.     procedure setPushed(Value : TBitMap);
  30.     procedure setDisabled(Value : TBitmap);
  31.     procedure doSound(whichSound: string);
  32.   protected
  33.     { Protected declarations }
  34.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  35.       X, Y: Integer); override;
  36.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
  37.       X, Y: Integer); override;
  38.     procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
  39.     procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  40.     procedure Paint; override;
  41.     procedure Click; override;
  42.  
  43.   public
  44.     { Public declarations }
  45.     constructor Create(AOwner: TComponent); override;
  46.     destructor Destroy; override;
  47.   published
  48.     { Published declarations }
  49.     property PicNormal   : TBitMap read FBmpNormal write setNormal;
  50.     property PicHiLight  : TBitMap read FBmpHiLight write setHiLight;
  51.     property PicPushed   : TBitMap read FBmpPushed write setPushed;
  52.     property PicDisabled : TBitmap read FBmpDisabled write setDisabled;
  53.  
  54.     property SndOver     : string read FSndOver write FSndOver;
  55.     property SndPush     : string read FSndPush write FSndPush;
  56.     property SoundType   : TSoundType read FSoundType write FSoundType;
  57.     property DLLInst     : integer read FDLLInstance write FDLLInstance;
  58.  
  59.     property Height default 30;
  60.     property Width default 30;
  61.  
  62.     property Enabled;
  63.     property Visible;
  64.  
  65.     property OnMouseDown;
  66.     property OnMouseMove;
  67.     property OnMouseUp;
  68.     property OnClick;
  69.  
  70.   end; //TMMButton
  71.  
  72. procedure Register;
  73.  
  74. implementation
  75. uses
  76.   MMSystem;
  77.  
  78. procedure Register;
  79.   begin
  80.     RegisterComponents('Additional', [TMMButton]);
  81.   end; //Register
  82.  
  83. constructor TMMButton.Create(AOwner: TComponent);
  84.   begin
  85.     inherited Create(AOwner);
  86.     Width := 30;
  87.     Height := 30;
  88.     FBmpNormal  := TBitMap.Create;
  89.     FBmpHiLight := TBitmap.Create;
  90.     FBmpPushed  := TBitmap.Create;
  91.     FBmpDisabled := TBitmap.Create;
  92.     FSoundType := stAppResource;
  93.     FDLLInstance := 0;
  94.   end;
  95.  
  96. destructor TMMButton.Destroy;
  97.   begin
  98.     FBmpNormal.Free;
  99.     FBmpHiLight.Free;
  100.     FBmpPushed.Free;
  101.     FBmpDisabled.Free;
  102.     inherited Destroy;
  103.   end;
  104.  
  105. procedure TMMButton.setNormal(Value: TBitMap);
  106.   begin
  107.     FBmpNormal.Assign(Value);
  108.     if not FBmpNormal.Empty then begin
  109.       //  Set the height and width of the component based on the
  110.       //  size of the Normal bitmap
  111.       Height := FBmpNormal.Height;
  112.       Width := FBmpNormal.Width;
  113.       FBmpNormal.PaletteModified := True;
  114.       end;
  115.   end; //setNormal
  116.  
  117. procedure TMMButton.setHiLight(Value: TBitMap);
  118.   begin
  119.     FBmpHiLight.Assign(Value);
  120.   end; //setHiLight
  121.  
  122. procedure TMMButton.setPushed(Value: TBitMap);
  123.   begin
  124.     FBmpPushed.Assign(Value);
  125.   end; //setPushed
  126.  
  127. procedure TMMButton.setDisabled(Value: TBitMap);
  128.   begin
  129.     FBmpDisabled.Assign(Value);
  130.   end; //setDisabled
  131.  
  132. procedure TMMButton.doSound(whichSound : string);
  133.   begin
  134.     case FSoundType of
  135.       stAppResource : PlaySound(PChar(whichSound), hInstance, SND_RESOURCE or SND_ASYNC or SND_NODEFAULT);
  136.       stDLLResource : PlaySound(PChar(whichSound), FDLLInstance, SND_RESOURCE or SND_ASYNC or SND_NODEFAULT);
  137.       stFile        : PlaySound(PChar(whichSound), 0, SND_FILENAME or SND_ASYNC or SND_NODEFAULT);
  138.     end; //case
  139.   end; //doSound
  140.  
  141. procedure TMMButton.MouseDown(Button: TMouseButton; Shift: TShiftState;
  142.   X, Y: Integer);
  143.   begin
  144.     FDown := True;
  145.     doSound(FSndPush);
  146.     if Not FBmpPushed.Empty then Paint;
  147.     inherited MouseDown(Button, Shift, X, Y);
  148.   end; //MouseDown
  149.  
  150. procedure TMMButton.CMMouseEnter(var Message: TMessage);
  151.   begin
  152.     FOver := True;
  153.     doSound(FSndOver);
  154.     if (Not FBmpHiLight.Empty) or FDown then Paint;
  155.   end; //CMMouseEnter
  156.  
  157. procedure TMMButton.CMMouseLeave(var Message: TMessage);
  158.   begin
  159.     FOver := False;
  160.     if (Not FBmpHiLight.Empty) or FDown then Paint;
  161.   end; //CMMouseLeave
  162.  
  163. procedure TMMButton.MouseUp(Button: TMouseButton; Shift: TShiftState;
  164.   X, Y: Integer);
  165.   var
  166.     DoClick: Boolean;
  167.   begin
  168.     FDown := False;
  169.     DoClick := (X >= 0) and (X < ClientWidth) and (Y >= 0) and (Y <= ClientHeight);
  170.     if DoClick then begin
  171.       Paint;
  172.       If Assigned(OnClick) then OnClick(Self);
  173.       end;
  174.     inherited MouseUp(Button, Shift, X, Y);
  175.   end; //MouseUp
  176.  
  177. procedure TMMButton.Click;
  178.   begin
  179.   end; //Click
  180.  
  181. // On 7/14/97, I added 7 lines of code to the paint method to allow the bitmaps colors to
  182. // be displayed correctly on a machine showing only 256 colors.
  183. //
  184. // Robert Vivrette - RobertV@csi.com
  185.  
  186. procedure TMMButton.Paint;
  187.   var
  188.     ARect : TRect;
  189.     Src   : TBitMap;
  190.     OldPal : HPalette;                                                          // RLV - 7/14/97
  191.  
  192.   begin
  193.     OldPal := SelectPalette(Canvas.Handle,FBmpNormal.Palette,False);            // RLV - 7/14/97
  194.     try                                                                         // RLV - 7/14/97
  195.       RealizePalette(Canvas.Handle);                                            // RLV - 7/14/97
  196.  
  197.       ARect := Rect(0,0,Width,Height);
  198.       if (csDesigning in ComponentState) then
  199.  
  200.         //Design-time paint response
  201.         if FBmpNormal.Empty then begin
  202.           //add visibility when designing
  203.           with Canvas.Pen do begin
  204.             Style := psSolid;
  205.             Color := clGray;
  206.             Mode :=  pmXor;
  207.             end;
  208.           Canvas.Brush.Style := bsClear;
  209.           Canvas.Rectangle(0, 0, Width, Height);
  210.           end
  211.         else
  212.           Canvas.CopyRect(ARect, FBmpNormal.Canvas, ARect)
  213.  
  214.       else begin  //Run-time paint response
  215.  
  216.         // Check the button state & assign the appropriate bitmap
  217.         if not Enabled then
  218.           Src := FBmpDisabled
  219.         else
  220.           if not FOver then
  221.             Src := FBmpNormal
  222.           else
  223.             if FDown then
  224.               Src := FBmpPushed
  225.             else
  226.               Src := FBmpHiLight;
  227.  
  228.         // catch all if the Src bitmap is not valid at this point, paint
  229.         // the normal bitmap.
  230.         if Src.Empty and (Not FBmpNormal.Empty) then
  231.           Src := FBmpNormal;
  232.  
  233.         // Paint the component's canvas
  234.         if not Src.Empty then
  235.           Canvas.CopyRect(ARect, Src.Canvas, ARect);
  236.  
  237.         end
  238.     finally                                                                     // RLV - 7/14/97
  239.       if OldPal <> 0 then                                                       // RLV - 7/14/97
  240.         SelectPalette(Canvas.Handle,OldPal,False);                              // RLV - 7/14/97
  241.     end;
  242.   end; //Paint
  243.  
  244. end.
  245.